package itdep.ui.pages.common.profile;

import ga.tapestry.StateConstants;
import ga.tapestry.commonlib.base.pages.AbstractPage;
import itdep.domain.profile.Profile;
import itdep.services.profile.ProfileService;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;

/**
 * @author Ivan Khalopik
 * @version $Revision: 58 $ $Date: 2010-07-08 23:36:22 +0000 (Thu, 08 Jul 2010) $
 */
public class ProfileAttachPicture extends AbstractPage {

	@Property
	private UploadedFile file;

	private Long profileId;

	@Inject
	private ProfileService profileService;

	@Component
	private Form attachForm;

	@InjectPage
	private ProfileDetails profileDetails;

	public Long getProfileId() {
		return profileId;
	}

	public void setProfileId(Long profileId) {
		this.profileId = profileId;
	}

	protected void onActivate(Long profileId) {
		this.profileId = profileId;
	}

	protected Long onPassivate() {
		return profileId;
	}

	protected Object onAttach() {
		if (attachForm.isValid() && file != null) {
			final Profile profile = profileService.get(profileId);
			if (profile != null) {
				profileService.attachPhoto(profile, file.getStream());
				profileDetails.setState(StateConstants.VIEW_STATE);
				profileDetails.setRecordId(profileId);
				return profileDetails;
			}
		}
		return null;
	}

}
